from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import DataRequired, ValidationError
from app.models import Admin

class LoginForm(FlaskForm):
    """管理员登入表单"""
    account = StringField(
        label="账号",
        validators=[DataRequired()],
        description="账号",
        render_kw={
            "class": "form-control",
            "placeholder": "请输入账号！"
        }
    )
    pwd = PasswordField(
        label="密码",
        validators=[DataRequired()],
        description="密码",
        render_kw={
            "class": "form-control",
            "placeholder": "请输入密码！"
        }
    )
    submit = SubmitField(
        label="登入",
        render_kw={
            "class": "btn btn-primary btn-block btn-flat"
        }
    )

'''
    LoginForm里定义一个validate_xxx函数，可自定义判断validate_on_submit()的值
    此判断完全可以写在 views.py login路由函数里，在'if xxx.validate_on_submit():' 之后加这么一层判断，
    避免两次查数据库。各种异常的提示全部由flash来抛出,就不用ValidationError来抛出了。
'''
    # def validate_account(self, field):
    #     account = field.data
    #     admin = Admin.query.filter_by(name=account).count()
    #     if admin == 0:
    #         raise ValidationError("账号不存在！")


class TagForm(FlaskForm):
    """标签表单"""
    name = StringField(
        label="名称",
        validators=[
            DataRequired("请输入标签名称！")
        ],
        description="标签",
        render_kw={
            "class": "form-control",
            "id": "input_name",
            "autofocus": "",
            "placeholder": "请输入标签名称！"
        }
    )
    submit = SubmitField(
        '编辑',
        render_kw={
            "class": "btn btn-primary"
        }
    )